<?php

function bc_half_details_gen( $productId ) {
	
	// Info on affiliate codes 
	// http://developer.ebay.com/DevZone/finding/CallRef/findItemsByKeywords.html#sampleaffiliate
	
	// Test Requests
	// http://developer.ebay.com/quickstartguide/sample/js/default.aspx
	
	// Pull all of these options from the Options Page
	$options = get_option('bc_options');
	extract($options);
	if( $security_app_name == '' )
		$security_app_name = 'JTWVentu-5405-49df-a2af-21c75dbaaa0f';
	
	$affiliate_networkid = '';
	$affiliate_trackingid = '';
	$affiliate_customid = '';

	// Construct Request
	$request = 'http://svcs.ebay.com/services/search/FindingService/v1?';
	$params = array (
		'OPERATION-NAME' => 'findItemsByProduct',
		'SERVICE-VERSION' => '1.11.0',
		'SECURITY-APPNAME' => $security_app_name,
		'RESPONSE-DATA-FORMAT' => 'XML',
		'REST-PAYLOAD' => '',
		'paginationInput.entriesPerPage' => '3',
		'productId' => $productId,
		'SortOrderType' => 'PricePlusShippingLowest'
		);
	
	if (bc_isbn_test($productId) == True) {
		// Clean up ISBN
		$params = bc_isbn_validator($params);
		$params["productId.@type"] = "ISBN";
	}
	else {
		$params["productId.@type"] = "UPC";
	}
	
	foreach ($params as $key => $value) {
		if ($value == '') 
			$request .= '&' . $key;
		elseif ($key == 'OPERATION-NAME')
			$request .= $key . '=' . $value;
		else
			$request .= '&' . $key . '=' . $value;
	}
		
	//echo $request; 
		
	// Make Request and get Response
	$response = @file_get_contents($request);
	$response = simplexml_load_string($response);
		
	// Populate the results array
	$bc_half_details = array();	

	if (isset($response->searchResult->item->viewItemURL))
		$bc_half_details['url'] = $response->searchResult->item->viewItemURL;
	if (isset($response->searchResult->item->sellingStatus->currentPrice))
		$bc_half_details['price'] = '$' . $response->searchResult->item->sellingStatus->currentPrice;
	else 
		return false;
	
	// So long as the values aren't blank return details
	return $bc_half_details;
}

?>